package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * 我的上传
 * Created by zxh on 16/11/25.
 */
public class MyUploadModel extends XListModel {

    private boolean articleSelected;
    private boolean videoSelected;

    @Bindable
    public boolean isArticleSelected() {
        return articleSelected;
    }

    public void setArticleSelected(boolean articleSelected) {
        this.articleSelected = articleSelected;
        if (articleSelected) {
            setVideoSelected(false);
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
        notifyPropertyChanged(BR.articleSelected);
    }

    @Bindable
    public boolean isVideoSelected() {
        return videoSelected;
    }

    public void setVideoSelected(boolean videoSelected) {
        this.videoSelected = videoSelected;
        if (videoSelected) {
            setArticleSelected(false);
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
        notifyPropertyChanged(BR.videoSelected);
    }

    public MyUploadModel(Context context) {
        super(context);
        getAdapter().setVariableId(BR.commonItem);
        super.getHandler().setTitle("我的上传");
        articleSelected = true;
    }

    @Override
    public void load(final int page) {
        final MyUploadModel self = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
//{"articleid":"2","title":"测试已","systime":"2016\/10\/24 15:15:07","browser":"2","collectcoun":"1"}
                ArrayList list = null;
                final XListAdapterInterface adapter = getAdapter();
                if (articleSelected) {
                    list = WebApi.getMyDocument(getContext(), page);
                    adapter.setVariableId(BR.commonItem);
                } else {
                    list = new ArrayList();
                }
                try {
                    for (Object message : list) {
                        LinkedTreeMap map = (LinkedTreeMap) message;
                        CommonListItem item = new CommonListItem(getContext(), R.layout.i_uploaded_article);
                        item.setTitle((String) map.get("title"));
                        item.setBrowser(Utils.getIntValue(map.get("browser")));
                        item.setCollectcoun(Utils.getIntValue(map.get("collectcoun")));
                        item.setSystime(Utils.getDateFromFormat(map.get("systime"), "yyyy/MM/dd HH:mm:ss"));
                        item.setId(Utils.getIntValue(map.get("articleid")));
                        item.setType(UploadItem.TYPE_ARTICLE);
                        adapter.add(item);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                if (list.size() != 0) {
                    setCurrentPage(page);
                }
                notifyDataSetChanged(list);
            }
        }).start();
    }

//    /**
//     * 点击上传文章
//     *
//     * @param view
//     */
//    public void onClickUploadArticle(View view) {
//        CommonFragment fragment = new CommonFragment();
//        ArticleEditModel model = new ArticleEditModel(getContext());
//        fragment.setVariableId(BR.editor);
//        fragment.setLayoutId(R.layout.f_send_article);
//        fragment.setDataModel(model);
//        HomeActivity.switchFragment((Activity) getContext(), fragment);
//    }
//
//    /**
//     * 点击上传视频
//     *
//     * @param view
//     */
//    public void onClickUploadVideo(View view) {
//        return;
//    }
}
